<template>
  <div>
    <el-dialog
      :title="mode==='add'?'新增服务':'编辑服务'"
      :visible.sync="dialogVisible"
      width="750px"
      :close-on-click-modal="false"
      @close="close"
    >
      <el-form ref="form" v-loading="loading" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="服务名称：" prop="title">
          <el-input v-model.trim="form.title" maxlength="10" show-word-limit />
        </el-form-item>
        <el-form-item label="服务说明：" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 6}"
            maxlength="200"
            show-word-limit
          />
        </el-form-item>
        <el-form-item label="是否默认：" prop="defaulted">
          <el-checkbox v-model="form.defaulted">设置为默认服务</el-checkbox>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :disabled="loading" size="small" round @click="hide">取 消</el-button>
        <el-button :disabled="loading" size="small" round type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addSupport, updateSupport } from '@/views/wxaMallModule/api/support'
export default {
  data() {
    return {
      // 校验规则
      rules: {
        title: [{ required: true, message: '请输入服务名称', trigger: 'blur' }],
        description: [
          { required: true, message: '请输入服务说明', trigger: 'blur' }
        ]
      },
      // 配置
      resolve: null,
      reject: null,
      // 状态
      dialogVisible: null,
      loading: false,
      // 数据
      mode: 'add',
      form: {
        type: 0,
        title: '',
        description: '',
        defaulted: false
      }
    }
  },
  methods: {
    /**
       * 弹框相关
       */
    open({ mode = 'add', row = '' }) {
      this.mode = mode
      if (row) this.form = Object.assign({}, row)
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    close() {
      this.loading = false
      this.form = {
        type: 0,
        title: '',
        description: '',
        defaulted: false
      }
      this.$refs.form.resetFields()
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    /**
       * 数据组
       */
    // 新增
    add(params) {
      addSupport(params)
        .then((res) => {
          this.$notify({
            title: '成功',
            message: '新增服务成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
          this.resolve && this.resolve()
          this.hide()
        })
        .catch((err) => {})
        .finally(() => {
          this.loading = false
        })
    },

    // 更新
    update(params) {
      updateSupport(params)
        .then((res) => {
          this.$notify({
            title: '成功',
            message: '更新服务成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
          this.resolve && this.resolve()
          this.hide()
        })
        .catch((err) => {})
        .finally(() => {
          this.loading = false
        })
    },

    /**
       * 按钮组
       */
    // 确 定
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.form.id) {
            this.update(this.form)
          } else {
            this.add(this.form)
          }
        }
      })
    }

    /**
       * 状态组
       */

    /**
       * 方法组
       */
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
